package com.meta.act.rest.controller.system;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.meta.act.app.service.SysPermissionService;
import com.meta.platform.file.config.OssServiceFactory;
import com.meta.platform.file.model.FileInfo;
import com.meta.platform.file.model.FileType;
import com.meta.platform.file.service.IFileService;
import com.meta.act.app.service.TenantQryService;
import com.meta.act.app.tenant.entity.TenantBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.meta.framework.common.constant.Constants;
import com.meta.framework.core.CommonRes;
import com.meta.act.entity.SysMenu;
import com.meta.framework.act.entity.SysUser;
import com.meta.framework.act.model.LoginBody;
import com.meta.framework.act.model.LoginUser;
import com.meta.framework.common.utils.ServletUtils;
import com.meta.act.app.service.SysLoginService;
import com.meta.framework.service.TokenService;
import com.meta.act.app.service.ISysMenuService;

import javax.annotation.Resource;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private OssServiceFactory fileServiceFactory;

    @Resource
    private TenantQryService tenantQryService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public CommonRes login(@RequestBody LoginBody loginBody)
    {
        CommonRes ajax = CommonRes.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);

        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public CommonRes getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 根据头像的文件ID查询文件url返回
        String fileType = FileType.ALIYUN.toString();
        IFileService fileService = fileServiceFactory.getFileService(fileType);
        List<FileInfo> avatarFiles = fileService.findFilesByIds(Arrays.asList(user.getAvatar()));
        if(avatarFiles != null && avatarFiles.size() > 0){
            user.setAvatar(avatarFiles.get(0).getUrl());
        }
        user.setTenantId(loginUser.getTenantId());


        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        CommonRes ajax = CommonRes.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        // 获取登录用户租户列表
        List<TenantBase> tenantList = tenantQryService.listTenantUserByUserId(user.getUserId());
        ajax.put("tenants", tenantList);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public CommonRes getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return CommonRes.success(menuService.buildMenus(menus));
    }
}
